package com.jt.assistant.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.afollestad.materialdialogs.list.listItems
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.jt.assistant.R
import com.jt.assistant.adapter.ReturnSkuAdapter
import com.jt.assistant.base.GlobalConfig
import com.jt.assistant.bean.Carton
import com.jt.assistant.bean.Goods
import com.jt.assistant.bean.Param
import com.jt.assistant.bean.ReturnSkuBean
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_seal_home.*

/**
 * 退货封箱查询  14902179  6941335026607 14940246  15158540
6941335009693
 */
class SealHomeActivity : BaseActivity<HomeViewModel>() {
    private val mAdapter by lazy {
        ReturnSkuAdapter(
            ArrayList<Goods>()
        )
    }
    private var mReturnSku: ReturnSkuBean? = null
    override fun layoutId(): Int = R.layout.activity_seal_home

    override fun initView(savedInstanceState: Bundle?) {
        with(rv_returnSku) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    override fun initData() {
    }

    @SuppressLint("SetTextI18n")
    override fun initListener() {
        set_sealSearch.setOnScanListener {
            if (it.isEmpty()) {
                ToastUtils.showShort("请输入渠道单号")
                return@setOnScanListener
            }

            val params = Param::class.java.newInstance()
            params.channelOrderNo = it
            viewModel.getReturnsSkuListByWaybill(params)
        }

        bt_commit.setOnClickListener {
            startActivity(Intent(this@SealHomeActivity, SealGoodsActivity::class.java).apply {
                putExtra("RETURNS_CODE", set_sealSearch.string)
                putExtra("RETURN_SKU_LIST", GsonUtils.toJson(mReturnSku))
            })
            finish()
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            listDialog(position)
        }

        viewModel.mGetReturnsSkuListByWaybill.observe(this, {
            if (it.isSuccess()) {
                mReturnSku = it.data
                mAdapter.data.clear()
                mAdapter.data.addAll(it.data.goodsList)
                mAdapter.notifyDataSetChanged()
                tv_packedCode.text = "已封${calculateTotalNumber(it.data.cartonList)}箱"
                tv_num.text = getSkuNumByGoods(it.data.goodsList)
                ll_status.visibility = View.VISIBLE
            } else {
                ll_status.visibility = View.GONE
            }
        })
    }

    private fun calculateTotalNumber(codes: ArrayList<Carton>): Int {
        if (codes.isEmpty()) return 0
        var maxNum = 0
        codes.forEach {
            val codeNum: Int = it.cartonCode.substring(12).toInt()
            if (codeNum > maxNum) maxNum = codeNum
        }
        return maxNum
    }

    private fun getSkuNumByGoods(goodsList: List<Goods>): String? {
        if (goodsList.isEmpty()) {
            return ""
        }
        var skuNum = 0
        var packedNum = 0
        goodsList.forEach {
            skuNum += it.totalNum
            packedNum += it.packedNum
        }

        bt_commit.visibility = if (packedNum == skuNum) View.GONE else View.VISIBLE
        return String.format("%d/%d", packedNum, skuNum)
    }


    private fun listDialog(position: Int) {
        val list = ArrayList<String>()

        val sku: String = mReturnSku!!.goodsList[position].sku

        for (carton in mReturnSku!!.cartonList) {
            if (carton.sku == sku) {
                list.add(carton.cartonCode)
            }
        }

        MaterialDialog(this).show {
            title(text = "箱码列表")
            listItems(
                items = list
            ) { _, indices, text ->
                dismiss()
            }
            positiveButton(text = "确  定")
            lifecycleOwner(this@SealHomeActivity)
        }
    }
}